package com.atguigu.gmall.seckill.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.model.activity.SeckillGoods;
import com.atguigu.gmall.mq.config.RabbitService;
import com.atguigu.gmall.seckill.pojo.SeckillOrder;
import com.atguigu.gmall.seckill.pojo.UserRecode;
import com.atguigu.gmall.seckill.service.SeckillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: dongdong lau
 * @CreateTime: 2021/7/15 10:26
 * @Description: 秒杀未付接口实现类
 */
@Service
public class SeckillServiceImpl implements SeckillService {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 根据时间查询时间所在时间段内所有秒杀商品
     *
     * @param time
     * @return
     */
    @Override
    public List<SeckillGoods> findAll(String time) {
        // 组装key eg: SeckillGoods_2021071512
        List<SeckillGoods> seckillGoods = redisTemplate.boundHashOps("SeckillGoods_" + time).values();
        // 返回结果
        return seckillGoods;
    }

    /**
     * 根据时间和商品Id查询商品详情
     *
     * @param time
     * @param id
     * @return
     */
    @Override
    public SeckillGoods getSeckillGoods(String time, String id) {
        // 组装key eg: SeckillGoods_2021071512
        SeckillGoods seckillGoods = (SeckillGoods) redisTemplate.boundHashOps("SeckillGoods_" + time).get(id);
        return seckillGoods;
    }

    @Autowired
    private RabbitService rabbitService;

    /**
     * 用户排队下单
     *
     * @param time
     * @param skuId
     * @param userId
     * @return
     */
    @Override
    public UserRecode addOrder(String time, String skuId, String userId) {
        // 记录用户的排队信息，将状态设置为排队中
        UserRecode userRecode = new UserRecode();
        // 创建redis队列防止重复排队
        Long increment = redisTemplate.boundValueOps("SeckillUserQueue_" + userId).increment(1);
        if(increment > 1){
            userRecode.setStatus(3);
            userRecode.setMsg("重复排队");
            return userRecode;
        }
        userRecode.setUsername(userId);
        userRecode.setCreateTime(new Date());
        userRecode.setStatus(1);
        userRecode.setGoodsId(skuId);
        userRecode.setNum(1);
        userRecode.setTime(time);
        // 将用户的的排队信息存入Redis
        redisTemplate.boundValueOps("SeckillStatus_" + userId).set(userRecode);
        // 发送消息进行异步下单
        rabbitService.sendMessage(
                "seckill_exchange",
                "seckill.order.add",
                JSONObject.toJSONString(userRecode));
        // 返回用户排队的结果
        return userRecode;
    }

    /**
     * 根据用户id查询排队状态
     *
     * @param userId
     * @return
     */
    @Override
    public UserRecode getUserRecode(String userId) {
        return (UserRecode) redisTemplate.boundValueOps("SeckillStatus_" + userId).get();
    }

    /**
     * 获取秒杀订单
     * @param username
     * @param orderId
     * @return
     */
    @Override
    public List<SeckillOrder> getSeckillOrder(String username, String orderId) {
        // 对订单Id进行判空
        if(StringUtils.isEmpty(orderId)){
            return redisTemplate.boundHashOps("SeckillOrder_" + username).values();
        }else{
            List<SeckillOrder> seckillOrders = new ArrayList<>();
            SeckillOrder o = (SeckillOrder) redisTemplate.boundHashOps("SeckillOrder_" + username).get(orderId);
            seckillOrders.add(o);
            return seckillOrders;
        }
    }
}
